<?php

/**
 * @author tuannguyen2042
 * @copyright 2013
 */

class YellComponent extends Object {
	
  public function getResults($loc_name, $cat_name, $proxy_ip, $proxy_port) {		            
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://www.yell.com/');
    curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
    curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
    curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);   //20 seconds
    curl_setopt($ch, CURLOPT_HEADER, 0);	
    $html = curl_exec($ch);
    
    if ($html === false){
      echo 'Proxy '.$proxy_ip.':'.$proxy_port.' die.<br/>';
      return false;
    }
      
    if (!preg_match('/<input.*?name="scrambleSeed"\s*value="(.*?)"/si', $html, $m)){            
      return 'error';
    }
		$scrambleSeed = $m[1];                
                
    $url = "http://www.yell.com/ucs/UcsSearchAction.do?".
		      "keywords=".urlencode($cat_name).
		      "&location=".urlencode($loc_name).
		      "&scrambleSeed=".urlencode($scrambleSeed).
		      "&searchType=&M=&bandedclarifyResults=&ssm=1";
    echo $url.'<br/>';
          
    curl_setopt($ch, CURLOPT_URL, $url);
		$html = curl_exec($ch);
            
    if ($html === false){
      echo 'Proxy '.$proxy_ip.':'.$proxy_port.' die.<br/>';
      return false;  
    }
		
    return $html;                                                                                                          		
	}
  
}


?>